package cn.itcast.Dao.impl;

import cn.itcast.Dao.RoleDao;
import cn.itcast.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wgq
 * @date 2019-09-20 18:38
 */
@Repository
public class RoleDaoImpl implements RoleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Role> findAllRoles() {
        List<Role> roleList = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    @Override
    public void save(Role role) {
        jdbcTemplate.update("insert into sys_role values(?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    @Override
    public List<Role> findRoleById(Long id) {
        List<Role> roleList = jdbcTemplate.query("SELECT * FROM sys_role sr, sys_user_role sur WHERE sr.id = sur.roleId AND sur.userId = ?",
                new BeanPropertyRowMapper<Role>(Role.class), id);
        return roleList;
    }
}
